<?php

/**
 * Admin Article Controller
 * Handle all article administration
 */

class Admin_ArticleController extends Zend_Controller_Action
{

    /**
     * Display last entered articles
     */
    public function indexAction() 
    {
        //fetch latest articles and output them
        $this->view->messages = $this->_helper->flashMessenger->getMessages();
    }

    /**
     * Display article form
     */
    public function newAction() 
    {
        $form = new Webote_Form_ArticleInput;
        $this->view->articleForm = $form;
    }

    /**
     * Save the article
     * we have 2 options: save and continue editing, and save and redirect to article index
     */
    public function saveAction()
    {
        $form = new Webote_Form_ArticleInput;
        
        if (!$this->getRequest()->isPost()) {
            $this->_helper->redirector->goto('index', 'article', 'admin');
            return;
        } elseif (!$form->isValid($_POST)) {
            $this->view->failedValidation = true;
            $this->view->loginForm = $form;
            return;
        }

        #save the article
        var_dump($_POST);
        
        $data = array(
            'title'      => $_POST['title'],
            'body' => $_POST['body'],
        );
        
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $db->insert('entries', $data);
        
        
        // if everything went well redirect to admin/article/index
        $this->_helper->FlashMessenger->addMessage('Article was successfully saved');
        $this->_helper->redirector->goto('index', 'article', 'admin');
    }

    /**
     * Display form for editing articles
     */
    public function editAction(int $id)
    {
        # code...
    }

    /**
     * Display confirmation, and if confirmed delete the article
     */
    public function deleteAction(int $id = null)
    {
        //delete, flashmessenger, forward to index,index,admin
    }

}